/*js基础工具方法*/

function Encrypt(word){
		 var key = CryptoJS.enc.Utf8.parse("1234123412ABCDEF");	
 
		 var srcs = CryptoJS.enc.Utf8.parse(word);
		 var encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
         return encrypted.toString();
	}
	function Decrypt(word){
		 var key = CryptoJS.enc.Utf8.parse("1234123412ABCDEF");	
 
		 var decrypt = CryptoJS.AES.decrypt(word, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
		 return CryptoJS.enc.Utf8.stringify(decrypt).toString();
	}
 

function getDAes(data){//解密
    var key  = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';  //密钥
    var iv   = '1234567812345678';
    var decryptedStr =getDAesString(data,key,iv);
    return decryptedStr;
}




// 从list中获取指定字段并以逗号分隔拼接为字符串
// list对象，index字段名
function getSplitArr(list,index){
	  var dataArr = [];
	  for(key in list){
		  dataArr.push(list[key][index]);
	  }
	  var dataArrStr = dataArr.join(",");
	  return dataArrStr;
  };
  

// 判断是否Json字符串
function isJsonStr(str) {
    if (typeof str == 'string') {
        try {
            var obj=JSON.parse(str);
            if(typeof obj == 'object' && obj ){
                return true;
            }else{
                return false;
            }
        } catch(e) {
            return false;
        }
    }
};

//判断是否为JSON对象
function isJSON(obj){
	 if(typeof(obj) == "object" && 
		  Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length){
		return true;
	 }else{
		return false;
	 }
}



// 判断是否为null
function isNotNull(ele){
	  if(ele===0){
		return true;
	  }
	  if(typeof ele==='undefined'){
	    return false;
	  }else if(ele==null){
	    return false;
	  }else if(ele==''){
	    return false;
	  }else if(new RegExp("^[ ]+$").test(ele)){
		return false;  
	  }
	  return true;
};  

// 是否数字
function isRealNum(val){
	　　if(val === "" || val == null){
	        return false;
	　　}
	   if(!isNaN(val)){　　　　
	　　　 return true; 　　
	   }else{ 　　
		 　　return false; 　
	   } 
};

// 是否价格数字格式
function isPriceNumber(_keyword){
	  if(_keyword == "0" || _keyword == "0." || _keyword == "0.0" || _keyword == "0.00"){
	      _keyword = "0"; return true;
	  }else{
	      var index = _keyword.indexOf("0");
	      var length = _keyword.length;
	      if(index == 0 && length>1){/* 0开头的数字串 */
	          var reg = /^[0]{1}[.]{1}[0-9]{1,2}$/;
	          if(!reg.test(_keyword)){
	              return false;
	          }else{
	              return true;
	          }
	      }else{/* 非0开头的数字 */
	          var reg = /^[1-9]{1}[0-9]{0,10}[.]{0,1}[0-9]{0,2}$/;
	          if(!reg.test(_keyword)){
	              return false;
	          }else{
	              return true;
	          }
	      }            
	      return false;
	  }
};

// 检查IP格式
function checkIP(ip){  
	 var re=/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
	 if(re.test(ip)){   
	     if( RegExp.$1<256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256) 
	     return true;   
	 }   
	 return false;    
};


// 时间字符串转date
function stringToDate(str){
    var tempStrs = str.split(" ");
    var dateStrs = tempStrs[0].split("-");
    var year = parseInt(dateStrs[0], 10);
    var month = parseInt(dateStrs[1], 10) - 1;
    var day = parseInt(dateStrs[2], 10);
    var timeStrs = tempStrs[1].split(":");
    var hour = parseInt(timeStrs [0], 10);
    var minute = parseInt(timeStrs[1], 10);
    var second = parseInt(timeStrs[2], 10);
    var date = new Date(year, month, day, hour, minute, second);
    return date;
};


/**
 * 自动将form表单封装成json对象
 */
$.fn.serializeObject = function() {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [ o[this.name] ];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};


// 给表单绑定将表单转换成json对象方法
$.fn.serializeJson=function(){  
    var serializeObj={};  
    var array=this.serializeArray();  
    var str=this.serialize();  
    $(array).each(function(){  
        if(serializeObj[this.name]){  
            if($.isArray(serializeObj[this.name])){  
                serializeObj[this.name].push(this.value);  
            }else{  
                serializeObj[this.name]=[serializeObj[this.name],this.value];  
            }  
        }else{  
            serializeObj[this.name]=this.value;   
        }  
    });  
    return serializeObj;  
};


Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, // month
        "d+": this.getDate(), // day
        "h+": this.getHours(), // hour
        "m+": this.getMinutes(), // minute
        "s+": this.getSeconds(), // second
        "q+": Math.floor((this.getMonth() + 3) / 3), // quarter
        "S": this.getMilliseconds() // millisecond
    }
    if (/(y+)/.test(format)) 
    	format = format.replace(RegExp.$1,(this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) 
    	if (new RegExp("(" + k + ")").test(format))
    		format = format.replace(RegExp.$1,RegExp.$1.length == 1 ? o[k] :("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

